package com.yiwei.zxp.view;

import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;

import com.yiwei.zxp.adapter.ItemTouchListener;

/**
 * Created by xsooy-pc on 18-5-23.
 */

public class MyItemTouchHelper extends ItemTouchHelper.Callback {

    private final ItemTouchListener mItemTouchListener;

    public MyItemTouchHelper(ItemTouchListener listener) {
        this.mItemTouchListener = listener;
    }


    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN; //开启上下
        int swipeFlags = ItemTouchHelper.START;//开启左右
//        int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;//开启左右

        return makeMovementFlags(dragFlags, swipeFlags);
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder viewHolder1) {

        if (viewHolder.getItemViewType() != viewHolder1.getItemViewType()) {
            return false;
            //不通之间的View不能拖动
        }

        mItemTouchListener.onItemMoved(viewHolder.getAdapterPosition(), viewHolder1.getAdapterPosition());

        return true;
    }


    @Override
    public boolean isLongPressDragEnabled() {
        return true;
    }


    @Override
    public boolean isItemViewSwipeEnabled() {
        return true;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int i) {
        mItemTouchListener.onItemDismiss(viewHolder.getAdapterPosition());
    }
}